package com.dongdongshop.controller;

import cn.hutool.core.util.RandomUtil;
import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnum;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.TbUserService;
import com.dongdongshop.shiro.ShiroUtils;
import com.dongdongshop.test.MyAuthenticator;
import org.apache.commons.lang3.RandomUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

@Controller
@RequestMapping("login")
public class LoginController {

    @DubboReference(check = false)
    private TbUserService userService;

    @RequestMapping("toLogin")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("loginUser")
    @ResponseBody
    public Result loginUser(String username, String password){
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
            subject.login(token);
        } catch (UnknownAccountException e) {
            return Result.response(ResultEnum.LOGIN_USERNAME_NULL);
        } catch (IncorrectCredentialsException e) {
            return Result.response(ResultEnum.LOGIN_PASSWORD_ERROR);
        }
        return Result.OK();
    }
    @RequestMapping("toEmilPwd")
    @ResponseBody
    public Result toEmilPwd(String username){
        try {
            TbUser user = userService.selectByUsername(username);
            String salt = ShiroUtils.generateSalt(6);
            String s1 = RandomUtil.randomNumbers(20);
            String s = ShiroUtils.encryptPassword("MD5",s1, salt, 3);
            user.setPassword(s);
            user.setSalt(salt);
            user.setUpdated(new Date());
            userService.updateUser(user);
            UserEmil(user.getEmail(),s1);
            return Result.OK();
        } catch (Exception e) {
            return Result.ERROR();
        }
    }

    private void UserEmil(String email,String pwd){
        //1.卫星
        Message message = null;

        //4.补充燃料
        Session session = null;

        //4.2
        Properties properties = null;
        properties = new Properties();
        //设置主机,端口,是否需要身份验证
        properties.put("mail.smtp.host","smtp.qq.com");
        properties.put("mail.smtp.port","25");
        properties.put("mail.smtp.auth","true");

        //4.1  //创建一个MyAuthenticator 用来实例化 Authenticator
        session = Session.getDefaultInstance(properties,
                new MyAuthenticator("1246218947@qq.com","djazlgcqnvdlhfhb"));

        //3.实例化message
        message = new MimeMessage(session);

        //设置消息的内容 3.1
        try {
            //设置邮件主题
            message.setSubject("修改密码");
            //设置邮件内容
            message.setText("密码为: "+ pwd + "");
            //设置邮件发送方
            InternetAddress address = new InternetAddress("1246218947@qq.com");
            message.setFrom(address);
            //设置邮件的接受方
            message.setRecipient(Message.RecipientType.TO,new InternetAddress(email));
            //2.送卫星上天
            Transport.send(message);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
